package com.yxy.gui;

import com.yxy.dao.DbManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class LoginFrame {
    private JFrame frame;
    private JPanel panel;
    private JLabel titleLabel, userLabel, passwordLabel, userTypeLabel;
    private JTextField userText;
    private JPasswordField passwordText;
    private JComboBox<String> userTypeComboBox;
    private JButton loginButton, resetButton, exitButton;

    public LoginFrame() {
        // 创建主窗口
        frame = new JFrame("学生选课系统");
        frame.setSize(600, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        // 创建面板
        panel = new JPanel(new GridBagLayout());
        frame.add(panel);
        placeComponents(panel);

        // 显示窗口
        frame.setVisible(true);
    }

    private void placeComponents(JPanel panel) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(10, 10, 10, 10); // 设置组件间距

        // 大标题
        titleLabel = new JLabel("学生选课系统", JLabel.CENTER);
        titleLabel.setFont(new Font("Serif", Font.BOLD, 36));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        panel.add(titleLabel, gbc);

        // 用户名标签
        userLabel = new JLabel("用户姓名：");
        userLabel.setFont(new Font("Serif", Font.PLAIN, 18));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        panel.add(userLabel, gbc);

        // 用户名输入框
        userText = new JTextField(20);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        panel.add(userText, gbc);

        // 密码标签
        passwordLabel = new JLabel("密码：");
        passwordLabel.setFont(new Font("Serif", Font.PLAIN, 18));
        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(passwordLabel, gbc);

        // 密码输入框
        passwordText = new JPasswordField(20);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        panel.add(passwordText, gbc);

        // 用户类型标签
        userTypeLabel = new JLabel("用户类型：");
        userTypeLabel.setFont(new Font("Serif", Font.PLAIN, 18));
        gbc.gridx = 0;
        gbc.gridy = 3;
        panel.add(userTypeLabel, gbc);

        // 用户类型下拉框
        String[] userTypes = {"学生", "教师", "管理员"};
        userTypeComboBox = new JComboBox<>(userTypes);
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        panel.add(userTypeComboBox, gbc);

        // 为按钮使用统一的 GridBagConstraints
        gbc.gridwidth = 1;
        gbc.fill = GridBagConstraints.NONE;

        // 登录按钮
        loginButton = new JButton("登录");
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.ipadx = 20;
        panel.add(loginButton, gbc);

        // 重置按钮
        resetButton = new JButton("重置");
        gbc.gridx = 1;
        gbc.gridy = 4;
        panel.add(resetButton, gbc);

        // 退出按钮
        exitButton = new JButton("退出");
        gbc.gridx = 2;
        gbc.gridy = 4;
        panel.add(exitButton, gbc);

        // 添加按钮事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    // 获取输入内容
                    String username = userText.getText();
                    String password = String.valueOf(passwordText.getPassword());
                    String userType = (String) userTypeComboBox.getSelectedItem();

                    // 调用数据库验证方法
                    if (verifyLogin(username, password, userType)) {
                        JOptionPane.showMessageDialog(frame, "登录成功!");
                        frame.dispose(); // 关闭登录窗口
                        new MainFrame(userType); // 打开主界面
                    } else {
                        JOptionPane.showMessageDialog(frame, "用户名或密码错误或用户类型不匹配!");
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(frame, "出现错误: " + ex.getMessage());
                }
            }
        });

        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userText.setText("");
                passwordText.setText("");
                userTypeComboBox.setSelectedIndex(0);
            }
        });

        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    private boolean verifyLogin(String username, String password, String userType) {
        String query = "";

        // 根据用户类型选择查询表
        switch (userType) {
            case "学生":
                query = "SELECT * FROM student WHERE name = ? AND password = ?";
                break;
            case "教师":
                query = "SELECT * FROM teacher WHERE name = ? AND password = ?";
                break;
            case "管理员":
                query = "SELECT * FROM admin WHERE name = ? AND password = ?";
                break;
        }

        try (Connection conn = DbManager.getConnection();
             PreparedStatement stmt = conn.prepareStatement(query)) {
            stmt.setString(1, username);
            stmt.setString(2, password);
            try (ResultSet rs = stmt.executeQuery()) {
                return rs.next(); // 如果结果集不为空，说明验证成功
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LoginFrame();
            }
        });
    }
}

